package com.pz.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.pz.mapper.UserMapper;
import com.pz.pojo.User;
import com.pz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：PZ
 * @Date: 2020/11/3 20 24
 */

@Service
public class UserServiceImpl implements UserService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserMapper userMapper;


    @Override
    public User login(String name, String password) {
        if (StrUtil.isEmpty(name)|| StrUtil.isEmpty(password)){
            return null;
        }
        Map<String,Object>map = new HashMap<String, Object>();
        map.put("name",name);
        map.put("password", DigestUtil.md5Hex(password));
        User user = userMapper.findUserByCondition(map);
        return user;
    }


//    @Resource
//    private UserMapper userMapper;


    @Override
    public Object insert(User user) {

        // valid
        if (user == null) {
            return  null;
        }

        userMapper.insert(user);
        return  true;
//        return ReturnT.success();
    }


    @Override
    public Object delete(int id) {
        int ret = userMapper.delete(id);
//        return ret>0?ReturnT.success():ReturnT.error();
        return ret>0;
    }


    @Override
    public Object update(User user) {
        int ret = userMapper.update(user);
        return ret>0;
    }


    @Override
    public User load(int id) {
        return userMapper.load(id);
    }


    @Override
    public Map<String,Object> pageList(int offset, int pagesize) {

        List<User> pageList = userMapper.pageList(offset, pagesize);
        int totalCount = userMapper.pageListCount(offset, pagesize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }
}
